﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGOP.ESB.Log;
using GU.WebERP.Model;
using Newtonsoft.Json;
using JnsFramework.Condition;
using System.Collections;
using GU.WebERP.DAL;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using GU.WebERP.Core.AjaxFx;
using GU.WebERP.Core.Utils;

namespace GU.WebERPApp.Ajax.Sys
{
    [AjaxFxClass("itm.sys.menu", "系统菜单API")]
    public class SysMenuMethods
    {
        [AjaxFxMethodAttribute("itm.sys.menu.add", "添加系统菜单")]
        public AjaxFxRspJson Add(HttpContext context)
        {
            DAL_Dim_Menu dal = new DAL_Dim_Menu();
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            try
            {
                Hashtable ht = ConvertHashTable.ToHashTable(HttpUtility.UrlDecode(context.Request["data"]));
                ht.Remove("MenuId");
                int count = dal.Insert(ht);
                if (count > 0)
                {
                    ajax.RspCode = 1;
                    ajax.RspMsg = "添加系统菜单成功!";
                }
                else
                {
                    ajax.RspCode = 0;
                    ajax.RspMsg = "添加系统菜单失败!";
                }
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "添加系统菜单失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.menu.edit", "编辑系统菜单")]
        public AjaxFxRspJson Edit(HttpContext context)
        {
            DAL_Dim_Menu dal = new DAL_Dim_Menu();
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            try
            {
                Hashtable ht = ConvertHashTable.ToHashTable(HttpUtility.UrlDecode(context.Request["data"]));
                string menuId = Convert.ToString(ht["MenuId"]);
                ht.Remove("MenuId");
                if (string.IsNullOrWhiteSpace(menuId)) throw new Exception("编辑菜单时MenuId为空！");
                ConditionBuilder condition = new ConditionBuilder();
                condition.And("MenuId").Equal(menuId);
                int count = dal.Update(ht, condition);
                if (count > 0)
                {
                    ajax.RspCode = 1;
                    ajax.RspMsg = "编辑系统菜单成功!";
                }
                else
                {
                    ajax.RspCode = 0;
                    ajax.RspMsg = "编辑系统菜单失败!";
                }
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "编辑系统菜单失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.menu.delete", "删除系统菜单")]
        public AjaxFxRspJson Delete(HttpContext context)
        {
            DAL_Dim_Menu dal = new DAL_Dim_Menu();
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            try
            {
                string menuId = Convert.ToString(context.Request["mid"]);
                if (string.IsNullOrWhiteSpace(menuId)) throw new Exception("删除菜单时MenuId为空！");
                ConditionBuilder condition = new ConditionBuilder();
                condition.And("MenuId").Equal(menuId);
                Hashtable ht = new Hashtable();
                ht.Add("Disabled", 1);
                int count = dal.Update(ht, condition);
                if (count > 0)
                {
                    ajax.RspCode = 1;
                    ajax.RspMsg = "删除系统菜单成功!";
                }
                else
                {
                    ajax.RspCode = 0;
                    ajax.RspMsg = "删除系统菜单失败!";
                }
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "删除系统菜单失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.menu.get", "获取所有系统菜单")]
        public AjaxFxRspJson GetAll(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            DAL_Dim_Menu dal = new DAL_Dim_Menu();
            try
            {
                var list = dal.GetAll();
                if (list == null) list = new List<Model_Dim_Menu>();
                //list.Add(new Model_Dim_Menu() { MenuId=0, MenuName="根菜单", ParentId=-1 });
                ajax.MiniUIJson = JsonConvert.SerializeObject(list, Formatting.None, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "获取所有系统菜单失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.menu.getsingle", "获取单个系统菜单")]
        public AjaxFxRspJson GetSingle(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            DAL_Dim_Menu dal = new DAL_Dim_Menu();
            try
            {
                int menuId = Convert.ToInt32(context.Request["mid"]);
                ajax.RspData.Add("data", JsonConvert.SerializeObject(dal.GetSingle(menuId)));
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "获取所有系统菜单失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        [AjaxFxMethodAttribute("itm.sys.menu.getmenubyids", "根据多个MenuId获取系统菜单")]
        public AjaxFxRspJson GetMenuByIds(HttpContext context)
        {
            AjaxFxRspJson ajax = new AjaxFxRspJson(context);
            DAL_Dim_Menu dal = new DAL_Dim_Menu();
            try
            {
                string menuIds = context.Request["mids"];
                ajax.MiniUI.Add("data", JToken.FromObject(ToUserMenu(dal.GetAllByIds(menuIds))));
            }
            catch (Exception ex)
            {
                ajax.RspCode = 0;
                ajax.RspMsg = "根据多个MenuId获取系统菜单失败!";
                LogManager.WriteAppError(ex);
            }
            return ajax;
        }

        public static List<MiniUi_Menu> ToUserMenu(IEnumerable<Model_Dim_Menu> list)
        {
            List<MiniUi_Menu> umList = new List<MiniUi_Menu>();
            foreach (Model_Dim_Menu item in list)
            {
                MiniUi_Menu um = new MiniUi_Menu();
                um.id = item.MenuId.ToString();
                um.pid = item.ParentId.ToString();
                um.text = item.MenuName;
                um.url = item.Link;
                um.display = item.Display;
                umList.Add(um);
            }
            return umList;
        }
    }
}